-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
43 lines (33 loc) · 1.05 KB
/
Solution.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <vector>
using namespace std;
int knapsack(int W, vector<int>& weights, vector<int>& values, int n) {
vector<vector<int>> dp(n + 1, vector<int>(W + 1, 0));
for (int i = 1; i <= n; i++) {
for (int w = 1; w <= W; w++) {
if (weights[i - 1] <= w) {
dp[i][w] = max(values[i - 1] + dp[i - 1][w - weights[i - 1]], dp[i - 1][w]);
} else {
dp[i][w] = dp[i - 1][w];
}
}
}
return dp[n][W];
}
int main() {
int n, W;
cout << "Enter the number of items: ";
cin >> n;
vector<int> weights(n), values(n);
cout << "Enter the weights and values of the items:" << endl;
for (int i = 0; i < n; i++) {
cout << "Item " << i + 1 << " - Weight: ";
cin >> weights[i];
cout << "Item " << i + 1 << " - Value: ";
cin >> values[i];
}
cout << "Enter the capacity of the knapsack: ";
cin >> W;
cout << "Maximum value in knapsack: " << knapsack(W, weights, values, n) << endl;
return 0;
}